package com.hsck.order.kds.main.fragment.noodles.batch

import android.view.LayoutInflater
import androidx.recyclerview.widget.GridLayoutManager
import com.hsck.order.kds.R
import com.hsck.order.kds.base.isnoam.log.XLog
import com.hsck.order.kds.base.isnoam.view.BaseActivity
import com.hsck.order.kds.base.isnoam.view.SpacesItemDecoration
import com.hsck.order.kds.base.utils.GsonUtils
import com.hsck.order.kds.databinding.ActivityBatchNoodleCuttingBinding
import com.hsck.order.kds.main.fragment.standard.bean.DishManager

/**
 * 粉面划菜-批量划菜
 * 按做法分类->标准做法-批量划菜、中辣-批量划菜
 */
class BatchNoodleCuttingActivity : BaseActivity<ActivityBatchNoodleCuttingBinding>() {
    override fun inflateBinding(inflater: LayoutInflater): ActivityBatchNoodleCuttingBinding {
        return ActivityBatchNoodleCuttingBinding.inflate(inflater)
    }

    private var batchNoodleCuttingAdapter: BatchNoodleCuttingAdapter? = null

    override fun initData() {
        super.initData()
        initRecyclerView()
        batchNoodleCuttingAdapter?.setListData(DishManager().getDishList())
        binding.viewBack.setOnClickListener { finish() }
    }

    private fun initRecyclerView() {
        batchNoodleCuttingAdapter = BatchNoodleCuttingAdapter(this)
        val gridLayoutManager = GridLayoutManager(this, 5)
        // 设置 SpanSizeLookup，来定义不同 ViewType 的占位
        gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                // 如果是 Header，则占据 5 列，否则占据 1 列
                return when (batchNoodleCuttingAdapter!!.getItemViewType(position)) {
                    BatchNoodleCuttingAdapter.TYPE_HEADER -> 5
                    BatchNoodleCuttingAdapter.TYPE_ITEM -> 1
                    else -> 1
                }
            }
        }
        binding.recyclerView.setItemViewCacheSize(20) // 提高缓存的视图数量
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = true
//        binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
//            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
//                super.onScrollStateChanged(recyclerView, newState)
//
//                // 滚动停止时
//                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    val firstVisiblePosition = (recyclerView.layoutManager as GridLayoutManager).findFirstVisibleItemPosition()
//
//                    // 如果当前显示的第一个位置大于 1，强制滑动到顶部
//                    if (firstVisiblePosition > 1) {
//                        recyclerView.smoothScrollToPosition(0)
//                    }
//                }
//            }
//        })


        binding.recyclerView.layoutManager = gridLayoutManager
        binding.recyclerView.adapter = batchNoodleCuttingAdapter
        val spacingInPixels = resources.getDimensionPixelSize(R.dimen.four_column_spacing)
        binding.recyclerView.addItemDecoration(
            SpacesItemDecoration(
                spacingInPixels, true
            )
        )
        batchNoodleCuttingAdapter!!.setDishOperationListener(object :
            BatchNoodleCuttingAdapter.DishOperationListener {
            override fun onDishOperationSelectedAction() {
                val selectedList = batchNoodleCuttingAdapter!!.getSelectedList()
                XLog.i(GsonUtils.o2J(selectedList))
            }
        })
        batchNoodleCuttingAdapter!!.setListData(DishManager().getDishList())
    }

    override fun onDestroy() {
        super.onDestroy()
        batchNoodleCuttingAdapter!!.stopAllUpdating()
    }
}